package com.diuse.outer.entity;



import com.diuse.outer.util.helper.BeanHelper;
import com.diuse.outer.util.helper.ClazzHelper;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;

/**
 * Created by Administrator on 2015/10/25 0025.
 */
public class BaseEntity implements Serializable {
    @Override
    public String toString() {
        List<Field> fields = ClazzHelper.getDeclaredFields(this.getClass());
        StringBuilder result = new StringBuilder(getClass().getSimpleName() + "{");

        for (int i = 0; i < fields.size(); i++) {
            Field field = fields.get(i);
            if (Modifier.isStatic(field.getModifiers())) {
                continue;
            }

            if (i > 0) {
                result.append(", ");
            }

            Object value = BeanHelper.getValue(this, field.getName());
            if (value!= null && value.getClass().isArray()) {
                Object[] arrayValue = (Object[]) value;
                String strValue = "[";
                for (Object v : arrayValue) {
                    strValue += v + ", ";
                }

                value = (strValue.length() > 1 ? strValue.substring(0, strValue.length() - 2) : strValue) + "]";
            }
            result.append(field.getName() + ": " + value);
        }
        result.append("}");

        return result.toString();
    }
}
